package com.lovo.ssc.controller;


import com.lovo.dto.TranslateDto;
import com.lovo.ssc.entity.Customer;
import com.lovo.ssc.entity.SysOrder;
import com.lovo.ssc.service.ICustomerService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/customer")
@Api(tags = "客户控制层002卢虹兵")
public class CustomerController {

    @Autowired
    ICustomerService customerService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("findAll")
    @ApiOperation(value = "查询全部客户")
    public List<Customer> findAll(){
        return customerService.list();
    }

    @PostMapping("saveCustomer")
    @ApiOperation(value = "添加客户")
    public boolean saveCustomer(Customer customer){
        return customerService.save(customer);
    }

    @PutMapping("updateState/{userName}/{state}")
    @ApiOperation(value = "修改状态")
    public void updateState (@PathVariable("userName") String userName,
                             @PathVariable("state") String state){
        customerService.updateState(userName, state);
    }

    @PutMapping("updateGrade/{userName}/{grade}")
    @ApiOperation(value = "修改等级")
    public void updateGrade (@PathVariable("userName") String userName,
                             @PathVariable("grade") String grade){
        customerService.updateGrade(userName, grade);
    }

    @GetMapping("findById/{customerId}")
    @ApiOperation(value = "根据id冻结客户")
    public boolean findById(@PathVariable("customerId") int customerId){

        Customer customer = customerService.getById(customerId);
        TranslateDto dto = new TranslateDto();
        dto.setRealName(customer.getRealname());
        dto.setUserName(customer.getUsername());
        dto.setUserSex(customer.getSex());
        dto.setUserPhone(customer.getPhone());
        dto.setUserQualification(customer.getImage());
        dto.setUserCard(customer.getSfzimg());
        dto.setUserPompany(customer.getCompanyname());
        dto.setGrade(customer.getGrade());
        dto.setState(customer.getState());
        if (dto!=null){
            rabbitTemplate.convertAndSend(MQUtil.frozenExchange,MQUtil.frozenKey,dto);
            return true;
        }
        return false;
    }

    @GetMapping("findOrderById/{customerId}")
    @ApiOperation(value = "根据客户Id查询订单")
    public List<SysOrder> findOrderById(@PathVariable("customerId") int customerId){
        return customerService.findOrderById(customerId);
    }
}
